<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\ApiController;
use App\Libraries\Error;
use App\Models\SalaryHistory;
use App\Models\User;
use Firebase\JWT\JWT;
use Illuminate\Http\Request;

class SalaryHistoryController extends ApiController
{


    public function create(Request $request) {
        if (!$request->token) {
            return showError(Error::ERR_ERROR, null, "无效的token!");
        }
        $userInfo = JWT::decode($request->token,env('JWT_KEY'), array('HS256'));
        $exp = $userInfo->exp;
        if (time() > $exp) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
        $userInfo = (array)$userInfo->data;
        $uid = $userInfo['uid'];
        $user = User::where('uid',$uid)->where('status',1)->first();
        if (empty($user)) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
//        $uid = '29221';
        $company_id = $userInfo['company_id'];


        if ( $request->id) {
            $salary_history = SalaryHistory::findOrFail($request->id);
            $salary_history->mtime = time();
        } else {
            $salary_history = new SalaryHistory();
            $salary_history->ctime = time();
        }
        $salary_history->employee_id = $request->employee_id;
        $salary_history->company_id = $company_id;
        $salary_history->year_month = $request->year_month;
        $salary_history->base_salary = $request->base_salary;
        $salary_history->post_salary = $request->post_salary;
        $salary_history->kpi_salary = $request->kpi_salary;
        $salary_history->traffic_salary = $request->traffic_salary;
        $salary_history->attendance_deduction = $request->attendance_deduction;
        $salary_history->wages_payable = $request->wages_payable;
        $salary_history->person_social_security = $request->person_social_security;
        $salary_history->person_accumulation_fund = $request->person_accumulation_fund;
        $salary_history->children_education = $request->children_education;
        $salary_history->continue_education = $request->continue_education;
        $salary_history->house_loan = $request->house_loan;
        $salary_history->housing_rent = $request->housing_rent;
        $salary_history->tax = $request->tax;
        $salary_history->paid_wages = $request->paid_wages;
        $salary_history->remark = $request->remark;
        $salary_history->uid = $uid;

        $salary_history->save();

        return showError(Error::ERR_OK,['id'=>$salary_history->id]);

    }

    public function show(Request $request) {
        $salary_history = SalaryHistory::findOrFail($request->id);
        return showError(Error::ERR_OK,['salary_history'=>$salary_history]);
    }

    public function index(Request $request)
    {
        if (!$request->token) {
            return showError(Error::ERR_ERROR, null, "无效的token!");
        }
        $userInfo = JWT::decode($request->token,env('JWT_KEY'), array('HS256'));
        $exp = $userInfo->exp;
        if (time() > $exp) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
        $userInfo = (array)$userInfo->data;
        $uid = $userInfo['uid'];
        $user = User::where('uid',$uid)->where('status',1)->first();
        if (empty($user)) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
//        $uid = '29221';
        $company_id = $userInfo['company_id'];
        // 搜索内容
        $where = [];
//        $name = $request->input('name');
//        if (!empty($name))
//        {
//            $where['name'] = $name;
//        }

        $where['company_id'] = $company_id;
        $where['status'] = 1;

        $list = SalaryHistory::getList($where);

        return showError(Error::ERR_OK,['list'=>$list]);
    }

    public function delete(Request $request) {
        SalaryHistory::where('id',$request->id)->update(['status'=>2,'mtime'=>time()]);
        return showError(Error::ERR_OK);
    }


}
